package cc.mrbird.febs.system.controller;

import cc.mrbird.febs.common.annotation.Log;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.system.entity.DetailInfo;
import cc.mrbird.febs.system.service.IDetailInfoService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 *  Controller
 *
 * @author MrBird
 * @date 2019-07-22 11:18:47
 */
@Slf4j
@Validated
@Controller
public class DetailInfoController extends BaseController {

    @Autowired
    private IDetailInfoService detailInfoService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "detailInfo")
    private String detailInfoIndex(){
        return FebsUtil.view("detailInfo/detailInfo");
    }

    @GetMapping("detailInfo")
    @RequiresPermissions("detailInfo:list")
    public FebsResponse getAllDetailInfos(DetailInfo detailInfo) {
        return new FebsResponse().success().data(detailInfoService.findDetailInfos(detailInfo));
    }

    @GetMapping("detailInfo/list")
    @ResponseBody
    public FebsResponse detailInfoList(QueryRequest request, DetailInfo detailInfo) {
        Map<String, Object> dataTable = getDataTable(this.detailInfoService.findDetailInfos(request, detailInfo));
        return new FebsResponse().success().data(dataTable);
    }

    @Log("新增DetailInfo")
    @PostMapping("detailInfo")
    @ResponseBody
    public FebsResponse addDetailInfo(@Valid DetailInfo detailInfo) throws FebsException {
        try {
            this.detailInfoService.createDetailInfo(detailInfo);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增DetailInfo失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("删除DetailInfo")
    @GetMapping("detailInfo/delete")
    @ResponseBody
    //@RequiresPermissions("detailInfo:delete")
    public FebsResponse deleteDetailInfo(DetailInfo detailInfo) throws FebsException {
        try {
            this.detailInfoService.deleteDetailInfo(detailInfo);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除DetailInfo失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("批量删除")
    @GetMapping("detailInfo/delete/{ids}")
    @ResponseBody
    public FebsResponse deleteDrawings(@NotBlank(message = "{required}") @PathVariable String ids) throws FebsException {
        try {
            String[] idss = ids.split(StringPool.COMMA);
            this.detailInfoService.batchDelete(idss);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("修改DetailInfo")
    @PostMapping("detailInfo/update")
    @RequiresPermissions("detailInfo:update")
    public FebsResponse updateDetailInfo(DetailInfo detailInfo) throws FebsException {
        try {
            this.detailInfoService.updateDetailInfo(detailInfo);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改DetailInfo失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @PostMapping("detailInfo/excel")
    @RequiresPermissions("detailInfo:export")
    public void export(QueryRequest queryRequest, DetailInfo detailInfo, HttpServletResponse response) throws FebsException {
        try {
            List<DetailInfo> detailInfos = this.detailInfoService.findDetailInfos(queryRequest, detailInfo).getRecords();
            ExcelKit.$Export(DetailInfo.class, response).downXlsx(detailInfos, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
